<?php
/**
 * Created by PhpStorm.
 * User: LG
 * Date: 2018/9/11
 * Time: 13:35
 */

header("content-type:text/html;charset=utf-8");         //设置编码
require_once 'Medoo.php';
require_once 'Contact.php';

/**
 *
 * 改变媒体开关状态类 GET
 * 前端入参：
 *  modularID:  模块开关ID
 *  switchID    模块下开关ID
 *  switchType  模块下开关ID的值
 */
$modularID = $_GET['modularID'];
$switchID = $_GET['switchID'];
$switchType = $_GET['switchType'];

stateChange();


/**
 * 筛选入参$switchID 对应字段
 */

function screenID($switchID)
{

    global $SWITCHID_MEDIA_SERVICE;
    global $SWITCHID_MEDIA_UI;
    global $SWITCHID_MEDIA_VIOCE;
    global $SWITCHID_MEDIA_CANCAT;
    global $SWITCHID_MEDIA_ALL_FINALDEVICE;
    global $SWITCHID_MEDIA_ALL_MEDIADEVICE;
    $fieldstr = "";

    switch ($switchID) {
        case $SWITCHID_MEDIA_SERVICE:
            $fieldstr = 'media_service';
            break;
        case $SWITCHID_MEDIA_UI:
            $fieldstr = 'media_ui';
            break;
        case $SWITCHID_MEDIA_VIOCE:
            $fieldstr = 'media_vioce';
            break;
        case $SWITCHID_MEDIA_CANCAT:
            $fieldstr = 'media_cancat';
            break;
        case $SWITCHID_MEDIA_ALL_FINALDEVICE:
            $fieldstr = 'media_all_finaldevice';
            break;
        case $SWITCHID_MEDIA_ALL_MEDIADEVICE:
            $fieldstr = 'media_all_mediadevice';
            break;
    }
    return $fieldstr;
}


/**
 * 查询该ID的开关状态
 */
function selectState($switchID)
{
    global $database;
    global $modularID;
    $fieldstr = screenID($switchID);//筛选ID

    $result = null;
    // 查询数据示例
    $datas = $database->select("manage_media_switch", [
        $fieldstr

    ], [
        "modularid[=]" => $modularID
    ]);

    foreach ($datas as $data) {
        $result = urlencode($data[$fieldstr]);
    }
    return $result;
}


/**
 * @return 判断是否有该条记录
 */
function hasRecoved()
{
    global $database;
    global $modularID;
    if ($database->has("manage_media_switch", array("modularid" => $modularID))) {
        return 1;
    } else {
        return 0;
    }
}


/**
 * @param $resultType 返回结果
 */
function returnResult($resultType)
{
    $resultMsg = "";
    $resultCode = "";
    if ($resultType == 1) {
        $resultCode = 200;
        $resultMsg = '修改成功';
    } else {
        $resultCode = 1;
        $resultMsg = '修改失败';
    }
    $result = array(
        'code' => urlencode($resultCode),
        'msg' => urlencode($resultMsg),
        'data' => null
    );

    echo urldecode(json_encode($result));
}


/**
 * 修改状态
 */
function stateChange()
{
    global $switchType;
    global $switchID;
    global $database;
    global $modularID;

    //只允许输入0和1
    if ($switchType != 0 && $switchType != 1) {
        returnResult(0);
        return;
    }

    $state = selectState($switchID); //获取返回状态
    $fieldstr = screenID($switchID);//筛选ID
    $hasRe = hasRecoved();//判断是否有该条记录

    if ($hasRe == 0) {   //执行插入
        $database->insert('manage_media_switch', [
            $fieldstr => $switchType,
            'modularid' => $modularID
        ]);
        returnResult(1);
//        var_dump($database->log());//medoo 的log日志

    } else if ($hasRe == 1) { //执行修改

        if ($state == $switchType) {

            returnResult(0);

        } else {
            $database->update("manage_media_switch", [
                $fieldstr => $switchType,
            ], [
                "modularid[=]" => $modularID
            ]);
            returnResult(1);
        }
    }

}
